package app.v1.yd.service.impl;

import app.common.utils.YdApi;
import app.v1.yd.domain.pojo.YdInventoryStatement;
import app.v1.yd.mapper.YdInventoryStatementMapper;
import app.v1.yd.service.YdInventoryStatementService;
import app.v1.yd.service.YdWarehouseService;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDResponseBody;
import com.gitee.feizns.dynamic.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author feizns
 * @since 2023/09/11
 */
@Service
@RequiredArgsConstructor
public class YdInventoryStatementServiceImpl implements YdInventoryStatementService {

    /**
     *
     */
    private final YdInventoryStatementMapper mapper;

    /**
     *
     */
    private final YdWarehouseService ydWarehouseService;

    /**
     * 从宜搭更新所有库存表
     */
    @Override
    public void updateAllInventoryStatementFromYiDa() {
        YdApi.inventoryStatement(this::insertOrUpdate);
    }

    /**
     * 从宜搭抓取指定时间之后更新的数据同步到旺店通
     * {@link String}
     */
    @Scheduled(cron = "0 0/6 * * * ?")
    public void reloadInventoryStatementUpdateToWdt() {
        try {
            LocalDateTime modifiedFormTime = LocalDateTime.now().minusHours(1);
            //抓取并更新到数据库
            YdApi.inventoryStatement(modifiedFormTime, this::insertOrUpdate);
            //抓取数据更新到旺店通
            reloadInventoryStatementUpdateToWdt(modifiedFormTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从宜搭抓取指定时间之后更新的数据同步到旺店通
     * @param modifiedFormTime {@link LocalDateTime}
     */
    @Override
    public void reloadInventoryStatementUpdateToWdt(LocalDateTime modifiedFormTime) {
        //抓取最新更新的数据
        List<YdInventoryStatement> statements = mapper.selectByCreateTimeAndLastModifiedTimeAfter(modifiedFormTime);
        //根据仓库名分组
        Map<String, List<YdInventoryStatement>> groupByWarehouseName = statements.stream()
                .collect(Collectors.groupingBy(YdInventoryStatement::getWarehouseName));
        //不同仓库的操作
        groupByWarehouseName.forEach(this::updateInventoryStatementToWdt);
    }

    /**
     * 更新指定仓库的数据库存数据到旺店通
     * @param warehouseName {@link String}
     * @param inventoryStatements {@link YdInventoryStatement}
     */
    @Override
    public void updateInventoryStatementToWdt(String warehouseName, List<YdInventoryStatement> inventoryStatements) {
        //仓库编号
        String no = YdWarehouseService.selectNoByName(warehouseName);
        //分隔数据为500条一组
        List<List<YdInventoryStatement>> lists = Lists.partition(inventoryStatements, 500);
        //将数据同步到旺店通
        lists.forEach(statement -> YdApi.wmsStockPdStockSyncByPdToWdt(no, statement));
    }

    /**
     * 将宜搭的数据解析后新增或插入到数据库中
     * @param data {@link GetFormDataByIDResponseBody}
     */
    public void insertOrUpdate(GetFormDataByIDResponseBody data) {
        //查询对应实例的库存表
        YdInventoryStatement ydInventoryStatement = mapper.selectByInstanceId(data.formInstId);
        //库存表为空则新建
        if ( ydInventoryStatement == null )
            ydInventoryStatement = new YdInventoryStatement();
        //将数据复制到库存表
        ydInventoryStatement.from(data);
        //过滤商家编码不为空的数据和非虚拟总仓的
        if ( StringUtils.hasText(ydInventoryStatement.getMerchantCode())
                && !"虚拟总仓".equals(ydInventoryStatement.getWarehouseName()) ) {
            //插入或更新数据
            ydInventoryStatement.insertOrUpdate();
        }
    }

}